package com.qiguliuxing.dts.db.service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.GmDocModMapper;
import com.qiguliuxing.dts.db.domain.GmDocMod;
import com.qiguliuxing.dts.db.domain.GmDocMod.Column;
import com.qiguliuxing.dts.db.domain.GmDocModExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class GmDocModService {
    Column[] columns = new Column[] { Column.id, Column.id, Column.docId,Column.docName, Column.modifyTime, Column.personId, Column.effectiveDate,
            Column.modifyReason, Column.content,Column.deleted};
    @Resource
    private GmDocModMapper docModMapper;

    /**
     * 获取热卖商品
     *
     * @return
     */
    public List<GmDocMod> queryByDid(Integer Did) {
        GmDocModExample example = new GmDocModExample();
        example.or().andDocIdEqualTo(Did).andDeletedEqualTo(false);
        example.setOrderByClause("modify_time desc");


        return docModMapper.selectByExample(example);
    }

/*    public List<GmDocMod> queryByGid(Integer id) {
        GmDocModExample example = new GmDocModExample();
        example.or().andIdEqualTo(id);
        return docModMapper.selectByExample(example);
    }*/

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
/*
    public List<GmDocMod> queryByNew(int offset, int limit) {
        GmDocModExample example = new GmDocModExample();
        example.or();
        example.setOrderByClause("modifyTime desc");
        PageHelper.startPage(offset, limit);

        return docModMapper.selectByExampleSelective(example, columns);
    }
*/

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<GmDocMod> queryByDocModId(List<Integer> catList, int offset, int limit) {
        GmDocModExample example = new GmDocModExample();
        example.or().andIdIn(catList);
        example.setOrderByClause("modify_time  asc");
        PageHelper.startPage(offset, limit);

        return docModMapper.selectByExampleSelective(example, columns);
    }

    /*/**
     * 获取分类下的商品
     *
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
    /*
     */
    /*
    public List<GmDoc> queryByCategory(Integer catId, int offset, int limit) {
        GmDocModExample example = new GmDocModExample();
        example.or().andCategoryIdEqualTo(catId).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return docModMapper.selectByExampleSelective(example, columns);
    }
*/

    public List<GmDocMod> querySelective(Integer Id, String docName, Integer personId,
                                      Integer offset, Integer limit, String sort, String order) {
        GmDocModExample example = new GmDocModExample();
        GmDocModExample.Criteria criteria1 = example.or();
        GmDocModExample.Criteria criteria2 = example.or();

        if (!StringUtils.isEmpty(Id) && Id != 0) {
            criteria1.andIdEqualTo(Id);
            criteria2.andIdEqualTo(Id);
        }
        if (!StringUtils.isEmpty(docName)) {
            criteria1.andDocNameEqualTo(docName);
            criteria2.andDocNameEqualTo(docName);
        }
        if (!StringUtils.isEmpty(personId)) {
            criteria1.andPersonIdEqualTo(personId);
            criteria2.andPersonIdEqualTo(personId);
        }


      /*  criteria1.andIseffectiveEqualTo(true);
        criteria2.andIseffectiveEqualTo(true);*/

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(offset, limit);

        return docModMapper.selectByExampleSelective(example, columns);
    }
/*
    public List<GmDoc> querySelective(String goodsSn, String name, Integer page, Integer size, String sort,
                                         String order,List<Integer> brandIds) {
        GmDocModExample example = new GmDocModExample();
        GmDocModExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (brandIds != null && brandIds.size() > 0) {
            criteria.andBrandIdIn(brandIds);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return docModMapper.selectByExampleWithBLOBs(example);
    }*/

    /**
     * 获取某个文档,包含完整信息
     *
     * @param id
     * @return
     */
    public GmDocMod findById(Integer id) {
        GmDocModExample example = new GmDocModExample();
        /*example.or().andidEqualTo(id).andDeletedEqualTo(false);*/
        example.or().andIdEqualTo(id);
        /* return goodsMapper.selectOneByExampleWithBLOBs(example);*/
        return docModMapper.selectOneByExampleSelective(example);
    }

  /*  *//**
     * 根据文档编号找到商品
     * @param docNumber
     * @return
     *//*
    public GmDocMod findByDoctype(String docTpe) {
        GmDocModExample example = new GmDocModExample();
        *//*example.or().andGoodsSnEqualTo(goodsSn).andDeletedEqualTo(false);*//*
        example.or().andidEqualTo(Id).andIseffectiveEqualTo(false);
        return docModMapper.selectOneByExampleSelective(example);
    }*/

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public GmDocMod findByIdVO(Integer id) {
        GmDocModExample example = new GmDocModExample();
        example.or().andIdEqualTo(id);
        return docModMapper.selectOneByExampleSelective(example, columns);
    }



/*    public int updateById(GmDoc goods) {
        goods.setUpdateTime(LocalDateTime.now());
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }*/

    public void deleteById(Integer id) {
        docModMapper.deleteByPrimaryKey(id);
    }

    public void deletByDid(Integer Did){
        GmDocModExample example = new GmDocModExample();
       example.or().andDocIdEqualTo(Did);
        docModMapper.logicalDeleteByExample(example);
    }


    public void add(GmDocMod docMod) {
        docMod.setModifyTime(LocalDateTime.now());
        /*doc.setUpdateTime(LocalDateTime.now());*/
        docModMapper.insertSelective(docMod);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     * 获取所有文档总数。
     * @return
     */
    public int count() {
        GmDocModExample example = new GmDocModExample();
        example.or();
        return (int) docModMapper.countByExample(example);
    }

/*    public List<Integer> getCatIds(Integer brandId, String keywords, Boolean isHot, Boolean isNew) {
        GmDocModExample example = new GmDocModExample();
        GmDocModExample.Criteria criteria1 = example.or();
        GmDocModExample.Criteria criteria2 = example.or();

        if (!StringUtils.isEmpty(brandId)) {
            criteria1.andBrandIdEqualTo(brandId);
            criteria2.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
            criteria1.andIsNewEqualTo(isNew);
            criteria2.andIsNewEqualTo(isNew);
        }
        if (!StringUtils.isEmpty(isHot)) {
            criteria1.andIsHotEqualTo(isHot);
            criteria2.andIsHotEqualTo(isHot);
        }
        if (!StringUtils.isEmpty(keywords)) {
            criteria1.andKeywordsLike("%" + keywords + "%");
            criteria2.andNameLike("%" + keywords + "%");
        }
        criteria1.andIsOnSaleEqualTo(true);
        criteria2.andIsOnSaleEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        criteria2.andDeletedEqualTo(false);

        List<GmDoc> goodsList = goodsMapper.selectByExampleSelective(example, Column.categoryId);
        List<Integer> cats = new ArrayList<Integer>();
        for (GmDoc goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;
    }*/

    public boolean checkExistByName(String name) {
        GmDocModExample example = new GmDocModExample();
        example.or().andDocNameEqualTo(name);
        return docModMapper.countByExample(example) != 0;
    }

    /**
     * 根据店铺，获取店铺对应类别的商品
     * 根据文档类型，获取对应文档类型的文档
     * @param brandId
     * @param i
     * @param related
     * @return
     */
/*    public List<GmDoc> queryByBrandId(int bid, int cid, int offset, int limit) {
        GmDocModExample example = new GmDocModExample();
        example.or().andBrandIdEqualTo(bid).andCategoryIdEqualTo(cid).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("browse desc");
        PageHelper.startPage(offset, limit);

        return docModMapper.selectByExampleSelective(example, columns);
    }*/

    /**
     * 同类商品，且不同店铺
     *
     * @param brandId
     * @param cid
     * @param i
     * @param limitCid
     * @return
     */
  /*  public List<GmDoc> queryByCategoryAndNotSameBrandId(int bid, int cid, int offset, int limit) {
        GmDocModExample example = new GmDocModExample();
        example.or().andBrandIdNotEqualTo(bid).andCategoryIdEqualTo(cid).andIsOnSaleEqualTo(true)
                .andDeletedEqualTo(false);
        example.setOrderByClause("browse desc");
        PageHelper.startPage(offset, limit);

        return docModMapper.selectByExampleSelective(example, columns);
    }*/
}
